﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "LineStyleDlg.h"

#include <QBoxLayout>
#include <QItemDelegate>
#include <QStandardItemModel>
#include <QPainter>
#include "GbmpNew.h"
#include "GmTitleBar.h"
#include "GmStyleSheetHelper.h"
#include "GbmpCommon.h"
#include <QComboBox>
#include "GraphicsStyleDlg.h"
#include "QColorDialog.h"
#include "IUiDocumentViewManager.h"
#include "IUiDocument.h"
#include "IUiView.h"
#include "ILineTypeNew.h"
#include "ILineTypeData.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;
using namespace std;

QWidget *LineStyleComboxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const  QModelIndex &index)  const
{
    QComboBox *editor =  NEW_AS_QT_CHILD(QComboBox, parent);
    editor->setStyleSheet("text-align:center;");
    FOR_EACH(itmStr, m_itemTextList)
    {
        editor->addItem(QString::fromStdWString(itmStr));
    }
    return  editor;
}

void  LineStyleComboxDelegate::setEditorData(QWidget *editor, const  QModelIndex &index)  const
{
    QString text = index.model()->data(index, Qt::EditRole).toString();
    QComboBox *comboBox = static_cast <QComboBox*>(editor);
    int  tindex = comboBox->findText(text);
    comboBox->setCurrentIndex(tindex);
}

void  LineStyleComboxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const  QModelIndex &index)  const
{
    QComboBox *comboBox = static_cast <QComboBox*>(editor);
    QString text = comboBox->currentText();
    model->setData(index, text, Qt::EditRole);
}

void  LineStyleComboxDelegate::updateEditorGeometry(QWidget *editor, const  QStyleOptionViewItem &option, const  QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}

void  LineStyleComboxDelegate::SetItemTextList(const std::vector<std::wstring>& itemList)
{
    m_itemTextList = itemList;
}



LineStyleDlg::LineStyleDlg(QWidget *parent/* = nullptr*/)
    : QDialog(nullptr, Qt::FramelessWindowHint | Qt::WindowSystemMenuHint
        | Qt::WindowMinMaxButtonsHint)
{
    m_lineType = GBMP_TR(L"无替换");
    m_lineWindth = -1;
    m_hasColorOverride = false;
    std::wstring styleSheetStr = GmStyleSheetHelper::Instance()->GetCurrentStyleData();
    setStyleSheet(QString::fromStdWString(styleSheetStr));
    m_ui.setupUi(this);
    m_ui.gbmp_title->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
    m_ui.gbmp_title->setIconVisible(false);
    m_ui.gbmp_title->setMaxButtonVisible(false);
    m_ui.gbmp_title->setMinButtonVisible(false);
    m_ui.gbmp_title->setText(QString::fromStdWString(GBMP_TR(L"线样式")));
    this->setProperty("use_gbmp_title", true);
    connect(m_ui.gbmp_title, &GmTitleBar::closeRequested, this, &LineStyleDlg::reject);
    connect(m_ui.pushButtonOk, &QPushButton::clicked, this, &LineStyleDlg::OnOK);
    connect(m_ui.pushButtonCancel, &QPushButton::clicked, this, &LineStyleDlg::OnCancle);
    m_pModel = NEW_AS_QT_CHILD(QStandardItemModel, this);
    m_pModel->setColumnCount(3);
    m_pModel->setHeaderData(0, Qt::Horizontal, QString::fromStdWString(GBMP_TR(L"线宽")));
    m_pModel->setHeaderData(1, Qt::Horizontal, QString::fromStdWString(GBMP_TR(L"线颜色")));
    m_pModel->setHeaderData(2, Qt::Horizontal, QString::fromStdWString(GBMP_TR(L"线型")));
    m_pModel->setHeaderData(0, Qt::Horizontal, QString::fromStdWString(L"线宽"), Qt::AccessibleDescriptionRole);
    m_pModel->setHeaderData(1, Qt::Horizontal, QString::fromStdWString(L"线颜色"), Qt::AccessibleDescriptionRole);
    m_pModel->setHeaderData(2, Qt::Horizontal, QString::fromStdWString(L"线型"), Qt::AccessibleDescriptionRole);
    m_pModel->setRowCount(1);
    QString viewVerHeader = QString("view[%1]").arg(0);
    m_pModel->setHeaderData(0, Qt::Vertical, viewVerHeader, Qt::AccessibleDescriptionRole);
    m_ui.tableView->setModel(m_pModel);
    m_ui.tableView->horizontalHeader()->setStretchLastSection(true);
    m_ui.tableView->verticalHeader()->setVisible(false);
    static std::wstring lineTypeArray[] = { L"无替换",L"1",L"2",L"3",L"4",L"5",L"6",L"7",L"9",L"10",L"11",L"12" };
    int lineTypeCount = sizeof(lineTypeArray) / sizeof(std::wstring);
    if (m_lineTypeList.size() == 0)
    {
        for (int i = 0; i < lineTypeCount; i++)
        {
            std::wstring str = lineTypeArray[i];
            if (str == L"无替换")
            {
                str = GBMP_TR(L"无替换");
            }
            m_lineTypeList.push_back(str);
        }
    }

    LineStyleComboxDelegate *pItemDelegateLineWidth = NEW_AS_QT_CHILD(LineStyleComboxDelegate, this);
    pItemDelegateLineWidth->SetItemTextList(m_lineTypeList);
    m_ui.tableView->setItemDelegateForColumn(0, pItemDelegateLineWidth);

    std::vector<wstring> lineTypeNames;
    lineTypeNames.push_back(GBMP_TR(L"无替换"));

    //std::vector<OwnerPtr<ILineType>> allLineTypes = ILineType::GetAllLineTypes();
    //FOR_EACH(pCurLineType, allLineTypes)
    //{
    //    DBG_WARN_AND_CONTINUE_UNLESS(pCurLineType != nullptr, L"LineType is null", L"GDMPLab", L"2023-12-20");
    //    lineTypeNames.push_back(pCurLineType->GetName());
    //}

    IUiDocumentViewManager* pUiDocumentViewManager = IUiDocumentViewManager::Get();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pUiDocumentViewManager, L"pUiDocumentViewManager为空",L"GDMPLab",L"2024-03-30");
    IUiView* pUIView = pUiDocumentViewManager->GetCurrentUiView();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pUIView, L"pUIView为空",L"GDMPLab",L"2024-03-30");
    IUiDocument* pUIDoc = pUIView->GetUiDocument();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pUIDoc, L"pUIDoc为空",L"GDMPLab",L"2024-03-30");
    IDocument* pDoc = pUIDoc->GetDbDocument();
    DBG_WARN_AND_RETURN_VOID_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");
    std::vector<ILineTypeNew*> lineTypes = ILineTypeNew::GetAllLineTypes(pDoc);
    FOR_EACH(pCurLineType, lineTypes)
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(pCurLineType, L"pCurLineType为空",L"GDMPLab",L"2024-03-30");
        const ILineTypeData* plnTypeData = pCurLineType->GetLineTypeData();
        lineTypeNames.push_back(plnTypeData->GetName());
    }

    LineStyleComboxDelegate *pItemDelegateLineType = NEW_AS_QT_CHILD(LineStyleComboxDelegate, this);
    pItemDelegateLineType->SetItemTextList(lineTypeNames);
    m_ui.tableView->setItemDelegateForColumn(2, pItemDelegateLineType);

    connect(m_ui.gbmp_title, &GmTitleBar::moveRequested, this, &LineStyleDlg::onMoveRequested);
    connect(m_ui.tableView, &QTableView::clicked, this, &LineStyleDlg::OnCellClicked);
    connect(m_ui.pushButtonClearReplase, &QPushButton::clicked, this, &LineStyleDlg::OnClearRepleaseClick);
}

void LineStyleDlg::Init()
{
    gcmp::OwnerPtr<QStandardItem> pItemLineWidth = NEW_AS_OWNER_PTR(QStandardItem, QString::fromStdWString(m_lineType));
    if (m_lineWindth == -1)
    {
        pItemLineWidth->setText(QString::fromStdWString(GBMP_TR(L"无替换")));
    }
    else
    {
        QString lineWidthStr = QString("%1").arg(m_lineWindth);
        pItemLineWidth->setText(lineWidthStr);
    }
    m_pModel->setItem(0, 0, TRANSFER_OWNERSHIP_TO_THIRD_PARTY(pItemLineWidth));
    gcmp::OwnerPtr<QStandardItem> pItemColor = NEW_AS_OWNER_PTR(QStandardItem, QString::fromStdWString(L""));
    pItemColor->setFlags(Qt::ItemIsEnabled);
    backGroundColor = pItemColor->background().color();
    if (m_hasColorOverride)
    {

        pItemColor->setBackground(QBrush(QColor(m_lineColor.R, m_lineColor.G, m_lineColor.B)));

    }
    else
    {
        pItemColor->setText(QString::fromStdWString(GBMP_TR(L"无替换")));
    }

    m_pModel->setItem(0, 1, TRANSFER_OWNERSHIP_TO_THIRD_PARTY(pItemColor));
    gcmp::OwnerPtr<QStandardItem> pItemLineType = NEW_AS_OWNER_PTR(QStandardItem, QString::fromStdWString(m_lineType));
    m_pModel->setItem(0, 2, TRANSFER_OWNERSHIP_TO_THIRD_PARTY(pItemLineType));
}

LineStyleDlg::~LineStyleDlg()
{

}

void LineStyleDlg::OnOK()
{
    QString lineWidth = m_pModel->item(0,0)->text();
    if (lineWidth == QString::fromStdWString(GBMP_TR(L"无替换")))
    {
        m_lineWindth = -1;
    }
    else
    {
        m_lineWindth = lineWidth.toInt();
    }

    QString lineType = m_pModel->item(0, 2)->text();
    m_lineType = lineType.toStdWString();
    QDialog::accept();
}

void LineStyleDlg::OnCancle()
{
    QDialog::reject();
}

void LineStyleDlg::onMoveRequested(const QPoint& cursorPos, const QPoint& offset)
{
    Q_UNUSED(cursorPos);
    move(pos() + offset);
}

void LineStyleDlg::OnCellClicked(const QModelIndex &index)
{
    if (index.column()==1)
    {
        QColorDialog dlg(this);
        dlg.setCurrentColor(QColor(0, 0, 0));
        if (dlg.exec() == (int)QDialog::Accepted)
        {
            m_hasColorOverride = true;
            QColor resColor = dlg.selectedColor();
            m_lineColor = gcmp::Color(resColor.red(), resColor.green(), resColor.blue());
            m_pModel->item(0, 1)->setText(QString::fromStdWString(L""));
            m_pModel->item(0, 1)->setBackground(QBrush(resColor));
        }
    }
}

void LineStyleDlg::OnClearRepleaseClick()
{
    m_hasColorOverride = false;
    m_lineColor = gcmp::Color(0,0,0);
    m_lineWindth = -1;
    m_lineType = GBMP_TR(L"无替换");
    m_pModel->item(0, 0)->setText(QString::fromStdWString(GBMP_TR(L"无替换")));
    m_pModel->item(0, 1)->setText(QString::fromStdWString(GBMP_TR(L"无替换")));
    m_pModel->item(0, 2)->setText(QString::fromStdWString(GBMP_TR(L"无替换")));
    m_pModel->item(0, 1)->setBackground(QBrush(QColor(255,255,255)));
}


double LineStyleDlg::GetLineWidth() const
{
    return m_lineWindth;
}

void LineStyleDlg::SetLineWidth(double lineWidth)
{
    m_lineWindth = lineWidth;
}

gcmp::Color LineStyleDlg::GetLineColor() const
{
    return m_lineColor;
}

void LineStyleDlg::SetLineColor(const gcmp::Color& color)
{
    m_lineColor = color;
}

bool LineStyleDlg::GetLineColorOverride() const
{
    return m_hasColorOverride;
}

void LineStyleDlg::SetLineColorOverride(bool isOverride)
{
    m_hasColorOverride = isOverride;
}

std::wstring LineStyleDlg::GetLineType() const
{
    return m_lineType;
}

void LineStyleDlg::SetLineType(const std::wstring& lineType)
{
    m_lineType = lineType;
}
